package com.yunquan.ohana.ui.selectFrames

import android.content.Intent
import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import com.google.gson.Gson
import com.yunquan.ohana.BR
import com.yunquan.ohana.R
import com.yunquan.ohana.base.ui.BaseActivity
import com.yunquan.ohana.databinding.ActivitySelectFramesBinding
import com.yunquan.ohana.entity.FramesEntity
import com.yunquan.ohana.ui.selectFrames.adapter.SelectFramesAdapter
import com.yunquan.ohana.ui.selectFrames.decoration.FramesHeaderDecoration

class SelectFramesActivity : BaseActivity<ActivitySelectFramesBinding, SelectFramesViewModel>() {


    override fun initContentView(savedInstanceState: Bundle?): Int {
        return R.layout.activity_select_frames
    }

    override fun initVariableId(): Int {
        return BR.selectFramesViewModel
    }

    override fun initData(savedInstanceState: Bundle?) {
        super.initData(savedInstanceState)
        binding!!.rvFrames.layoutManager = LinearLayoutManager(this)
        binding!!.rvFrames.addItemDecoration(FramesHeaderDecoration(this))
    }

    override fun initViewObservable() {
        super.initViewObservable()
        binding!!.llSelectAll.setOnClickListener {
            if (viewModel!!.mCheckedFrames.size == viewModel!!.mFramesData.size) {
                viewModel!!.mCheckedFrames.clear()
                viewModel!!.mFramesData.map { e -> e.check = false }
                binding!!.ivSelectAll.setImageResource(R.mipmap.ic_check2_false)
            } else {
                viewModel!!.mCheckedFrames.clear()
                viewModel!!.mFramesData.map { e -> e.check = true }
                viewModel!!.mCheckedFrames.addAll(viewModel!!.mFramesData)
                binding!!.ivSelectAll.setImageResource(R.mipmap.ic_check2_true)
            }
            val adapter = binding!!.rvFrames.adapter as SelectFramesAdapter
            adapter.notifyDataSetChanged()
        }
        binding!!.btnNext.setOnClickListener {
            if (viewModel!!.mCheckedFrames.isEmpty()) {
                return@setOnClickListener
            }
            val jsonData = Gson().toJson(viewModel!!.mCheckedFrames)
            val bundle = Bundle()
            bundle.putString("frames", jsonData)
            val intent = Intent()
            intent.putExtras(bundle)
            setResult(RESULT_CODE, intent)
            finish()
        }
        viewModel!!.ui.observe(this) {
            when (it) {
                0 -> {
                    val adapter = SelectFramesAdapter(true, viewModel!!.mFramesData)
                    adapter.setOnItemCheckedListener(object : SelectFramesAdapter.OnItemCheckedListener {
                        override fun onItemChecked(check: Boolean, item: FramesEntity) {
                            val index = viewModel!!.mCheckedFrames.indexOfFirst { e -> e.id == item.id }
                            if (check) {
                                if (index < 0) {
                                    viewModel!!.mCheckedFrames.add(item)
                                }
                            } else {
                                if (index >= 0) {
                                    viewModel!!.mCheckedFrames.removeAt(index)
                                }
                            }
                            if (viewModel!!.mCheckedFrames.size == viewModel!!.mFramesData.size) {
                                binding!!.ivSelectAll.setImageResource(R.mipmap.ic_check2_true)
                            } else {
                                binding!!.ivSelectAll.setImageResource(R.mipmap.ic_check2_false)
                            }
                        }
                    })
                    binding!!.rvFrames.adapter = adapter
                    binding!!.rvFrames.requestLayout()
                    binding!!.rvFrames.invalidate()
                }
            }
        }
    }

    companion object {
        const val RESULT_CODE = 1005
    }
}